import config from './config.js';
import { showToast } from '@/common/interactiveFeedback.js'
export default {
	request (options = {}) {
		return new Promise((resolve, reject) => {
			const token = uni.getStorageSync("token")

			options.url = config.baseUrl + options.url
			options.header.token = token;
			options.complete = (response) => {
				console.log("🚀 ~ options.complete ~ response:", response)
				if (response.statusCode == 200 || response.statusCode == 0) {
					if (response.data.code == 500 || response.data.code == 0) {
						showToast(response.data.msg)
					}
					resolve(response.data)
				} else {
					if (response.data.code == 401) {
						uni.removeStorageSync('user_info');
						// uni.removeStorageSync('token');
					}
					showToast(response.data.msg)
				}
			}

			uni.request(options)
		})
	},

	post (url, data = {}, header = {}) {

		let options = {
			url: url,
			data: data,
			header,
			// header: { ...header, 'Content-Type': 'application/x-www-form-urlencoded' },
			method: "POST"
		}

		return this.request(options);
	},

	get (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}

		return this.request(options);
	}
};